<?php
namespace app\backstage\controller;
use app\backstage\model\Link;
use app\common\classes\ReportOrder;
use think\Controller;
use think\Db;
use think\facade\Request;

class Backhaul extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    public function index()
    {
        $Account = \think\facade\App::model('Account', 'logic');
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;

        $channels = $Account->getChannels($agentId);

        $this->assign('advert_medias',advertMediaList());
        $this->assign('channels', $channels);
        $this->assign('role_name', request()->_role->alias);
        return $this->fetch();
    }

    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Backhaul = \think\facade\App::model('Backhaul', 'logic');
        $result = $Backhaul->getList($params);

        return sucJson($result['list'], $result['count']);
    }

    public function add()
    {
        if( Request::isGet() ){

            $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
            $Account = \think\facade\App::model('Account', 'logic');
            $channels = $Account->getChannels($agentId);

            $this->assign('loop_ratios',getBackhaulLoopRatio());
            $this->assign('report_caliber_list',getReportCaliberList());
            $this->assign('advert_medias',advertMediaList());
            $this->assign('channels',$channels);
            $this->assign('role_name',request()->_role->alias);
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');

        if(request()->_role->alias == 'channel')
        {
            $verify_result = $this->validate($params, 'BackhaulLink.add_channel');
        }else{
            $verify_result = $this->validate($params, 'BackhaulLink.add');
        }

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Backhaul = \think\facade\App::model('Backhaul','logic');

        $data   = $Backhaul->dataProcessing($params);
        $Backhaul->addInfo($data['data']);

        return ['code'=>100,'msg'=>'操作成功'];
    }

    public function preview()
    {
        $params = Request::param('', '', 'strip_tags');

        $where[] = ['id','=',$params['id']];

        $Backhaul = \think\facade\App::model('Backhaul','logic');
        $info = $Backhaul->getInfo($where);

        if(!$info){
            $this->error('该信息记录不存在');
        }

        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('loop_ratios',getBackhaulLoopRatio());
        $this->assign('report_caliber_list',getReportCaliberList());
        $this->assign('advert_medias',advertMediaList());
        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function edit()
    {
        $params = Request::post('','','strip_tags,trim');


        if(request()->_role->alias == 'channel')
        {
            $verify_result = $this->validate($params, 'BackhaulLink.edit_channel');
        }else{
            $verify_result = $this->validate($params, 'BackhaulLink.edit');
        }

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Backhaul = \think\facade\App::model('Backhaul','logic');

        $where_u[] = ['id','=',$params['id']];

        if (request()->_role->alias == 'agent') {

            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                ->column('id');

            if(!$channel_ids){
                return json(['code'=>101,'msg'=>'该记录未归属给该代理，请核实后操作']);
            }

            $where_u[] = ['channel_id', 'in', $channel_ids];
        }

        if(request()->_role->alias == 'channel')
        {
            $where_u[] = ['channel_id','=',request()->_user->id];
        }

        $data   = $Backhaul->dataProcessing($params);
        $result = $Backhaul->editInfo($where_u,$data['data']);

        return json($result);
    }

    /*数据删除*/
    public function delete()
    {
        $params = Request::param();
        $verify_result = $this->validate($params, 'BackhaulLink.info');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Backhaul = \think\facade\App::model('Backhaul','logic');

        $where[] = ['id','in',$params['id']];

        if (request()->_role->alias == 'agent') {

            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                ->column('id');

            if(!$channel_ids){
                return json(['code'=>101,'msg'=>'该记录未归属给该代理，请核实后操作']);
            }

            $where[] = ['channel_id', 'in', $channel_ids];
        }

        if(request()->_role->alias == 'channel')
        {
            $where[] = ['channel_id','=',request()->_user->id];
        }

        $result  = $Backhaul->delInfo($where);

        return json($result);
    }

    /*回传记录*/
    public function log()
    {
        $this->assign('backhaul_types',getBackhaulTypeList());
        return $this->fetch();
    }

    /*回传记录列表数据*/
    public function log_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Log = \think\facade\App::model('BackhaulLog', 'logic');
        $result = $Log->getList($params);

        if ($result['count'] > 0) {
            return ['code' => 0, 'msg' => '数据获取成功', 'count' => $result['count'], 'data' => $result['list']];
        }

        return ['code' => 1, 'msg' => '暂无数据', 'count' => $result['count'], 'data' => $result['list']];
    }

    /*完整链接路径获取*/
    public function link_full()
    {
        $params = Request::param('', '', 'strip_tags');
        $Link = \think\facade\App::model('Link', 'logic');
        $result = $Link->getLinkPathFull($params);
        if($result['code'] != 100)
        {
            $this->error($result['msg']);
        }
        $this->assign('info',$result['data']);
        return $this->fetch();
    }

    /*手动激活用户*/
    public function manual_active()
    {

        if( Request::isGet() ) {
            $this->assign('advert_medias', advertMediaList());
            return $this->fetch();
        }

        $params = Request::param('', '', 'strip_tags');
        $verify_result = $this->validate($params, 'BackhaulLink.user_active');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $user = Db::table('quick_user')->where('id',$params['uid'])
            ->where('channel_id',request()->_user->id)
            ->field('quick_id,channel_id,id,regtime')->findOrEmpty();

        if(empty($user))
        {
            return json(['code'=>101,'msg'=>'该用户ID不存在，请重新输入']);
        }

        $link['advert_media_id'] = $params['advert_media_id'];

        $ReportOrder = new ReportOrder();
        $result = $ReportOrder->manual_active($link,$user);

        return json($result);
    }

}